<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Database;
use brand\page\Table;

class Select extends Component
{
    public function _init()
    {
        $this->component = 'select';
    }




    public function Table($this_column)
    {

        parent::Table($this_column);

        $show_type = $this->getTableParam('show_type') ?: '';
        if($show_type == 'curd' || $show_type == ''){
            //列表只支持单选
            if (isset($this->config['table']['options']) && $this->config['table']['options'] && (!isset($this->config['table']['multiple']) || !$this->config['table']['multiple'])){

                $this_column = $this_column->scopedSlots(
                    [
                        Table::$SurfaceTable->select()->props(
                            [
                                'async'   => ['method' => 'post', 'data' => [$this->pk], 'url' => Table::$update_url],
                                'options' => $this->config['table']['options'],
                            ]
                        ),
                    ]
                );
            }
        }




        return $this_column;

    }

    /**
     * 表单值字段过滤
     */
    public function checkFormValue()
    {
        if ($this->getFormParam('props.multiple')){
            $this->value = BrandHelper::string2array($this->value);
        }

    }

    /**
     * table表格内容
     * @param $key
     * @param $value
     */
    public function checkTableValue($key, $value)
    {

        $value = parent::checkTableValue($key, $value);
        $show_type = $this->getTableParam('show_type') ?: '';

        if ($this->getTableParam('options') && $this->getTableParam('multiple')) {

            $value = BrandHelper::arrayFindInArray(BrandHelper::string2array($value),$this->getTableParam('options'));

        }elseif($show_type == 'options'){
            $value = $this->getTableParam('options')[$value] ?? "";
        }

        return $value;
    }

    /**
     * 插入验证
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        return BrandHelper::array2String($value);
    }


    /**
     * 创建Curd表单
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        $column = \brand\Brand::Builder()->form_props($props);
        $comment = Database::curdComment($field['comment']);

        if($comment['params']){

            $column->form_method('select')
                ->form_options($comment['params'])
                ->table_action_type('select')
                ->table_options($comment['options'] ?? $comment['params'] );

            if (isset($props['multiple']) && $props['multiple']){
                $column->form_props(['multiple'=>true])->table_multiple(true);
            }else{
                $column->search_condition('=');
            }
        }

        return $column;

    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        return $this->buildSelectConfig($tableConfigValue,'Select',$curd_method);
    }

    public function methodFormConf($form,$table,$params)
    {
        $select_multiple = request()->param('select_multiple',0);
        $param = $params['param'] ?? [];
        $param = BrandHelper::optionsStrToInt($param);
        $form['options'] = $param;
        $form['props']['multiple'] = $select_multiple == 1;
        $table['action_type'] = 'select';
        $table['options'] = BrandHelper::encodeOptions($param);

        return [$form,$table];
    }

}